package com.lihepeng.leecode.juc;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 验证子线程获取锁之后主线程是不是等待 还是自动获取
 * 只有自己的锁 才可以冲入
 */
public class ReentrantLockDemo {
    private ReentrantLock reentrantLock = new ReentrantLock();
    private  int total =3;
    public static void main(String[] args) throws InterruptedException {
        ReentrantLockDemo reentrantLockDemo = new ReentrantLockDemo();
        System.out.println("我是主线程");
        new Thread(()->{
            reentrantLockDemo.lockFunc();
        },"thread-1").start();
    }
    public void lockFunc(){
        if (total-- ==0){
            return;
        }

        reentrantLock.lock();
        try {
            System.out.println(Thread.currentThread().getName()+"exec");
            lockFunc();
            TimeUnit.SECONDS.sleep(1);
            System.out.println(Thread.currentThread().getName()+"finish");

        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            reentrantLock.unlock();
        }

    }
}
